package dao.impl;

import dao.BaseDao;
import dao.FlowerDao;
import entity.Flower;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: DELL
 * @Since: 2024/9/29 09:44
 * @Description:
 */
public class FlowerDaoImpl extends BaseDao implements FlowerDao {
    Flower flower=null;
    @Override
    public List<Flower> getAll() {
        String sql="SELECT id,name,anothername,property,price,production FROM flower";
        ResultSet rs=executeQuery(sql);
        List<Flower> list=new ArrayList<>();
        try{
            while (rs.next()){
                flower=new Flower();
                flower.setId(rs.getInt("id"));
                flower.setName(rs.getString("name"));
                flower.setAnothername(rs.getString("anothername"));
                flower.setProperty(rs.getString("property"));
                flower.setPrice(rs.getDouble("price"));
                flower.setProduction(rs.getString("production"));
                list.add(flower);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(connection,pstmt,rs);
        }
        return list;
    }

    @Override
    public int add(String name, String anothername, String property, double price, String production) {
        String sql="insert into flower(name,anothername,property,price,production) values(?,?,?,?,?)";
        Object params[]={name,anothername,property,price,production};
        int hangshu=executeUpdate(sql,params);
        return hangshu;
    }

    @Override
    public int delete(int id) {
        String sql="delete from flower where id=?";
        Object params[]={id};
        int hangshu=executeUpdate(sql,params);
        return hangshu;
    }

    @Override
    public int update(int id, String name, String anothername, String property, double price, String production) {
        String sql="update flower set name=?,anothername=?,property=?,price=?,production=? where id=?";
        Object params[]={name,anothername,property,price,production,id};
        int hangshu=executeUpdate(sql,params);
        return hangshu;
    }
}
